package com.lmf.mock.starter.autoconfigure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmf.mock.starter.autoconfigure.entity.ProxyGroup;
import com.lmf.mock.starter.autoconfigure.mapper.ProxyGroupMapper;
import com.lmf.mock.starter.autoconfigure.service.ProxyGroupService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 代理分组服务实现
 */
@Service
public class ProxyGroupServiceImpl extends ServiceImpl<ProxyGroupMapper, ProxyGroup> implements ProxyGroupService {

    @Override
    public List<ProxyGroup> getEnabledGroups() {
        QueryWrapper<ProxyGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("enabled", true);
        wrapper.orderByAsc("create_time");
        return list(wrapper);
    }

    @Override
    public ProxyGroup getGroupById(Long id) {
        return getById(id);
    }

    @Override
    public ProxyGroup createGroup(ProxyGroup group) {
        group.setCreateTime(LocalDateTime.now());
        group.setUpdateTime(LocalDateTime.now());
        if (group.getEnabled() == null) {
            group.setEnabled(true);
        }
        save(group);
        return group;
    }

    @Override
    public ProxyGroup updateGroup(ProxyGroup group) {
        group.setUpdateTime(LocalDateTime.now());
        updateById(group);
        return group;
    }

    @Override
    public boolean deleteGroup(Long id) {
        return removeById(id);
    }

    @Override
    public boolean toggleGroupStatus(Long id) {
        ProxyGroup group = getById(id);
        if (group != null) {
            group.setEnabled(!group.getEnabled());
            group.setUpdateTime(LocalDateTime.now());
            return updateById(group);
        }
        return false;
    }
}
